package com.wiz.web.controllor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.common.ResponseCode;
import com.wiz.service.MemberService;
import com.wiz.to.MemberTO;
import com.wiz.vo.CollectionVO;
import com.wiz.vo.ResponseJsonBody;
import com.wiz.vo.criteria.MemberSearchCriteriaTO;
/**
 * 会员管理
 * @author xxy
 *
 */
@Controller
@RequestMapping("/member")
public class MemberControllor implements ResponseCode{
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	@Resource
	private MemberService memberService;
	
	
	@RequestMapping(value="/list")
	public String getFuelingListByCriteriaTO(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "numPerPage", required = false,defaultValue="10") int pageSize,
			@RequestParam(value = "pageNum", required = false,defaultValue="1") int pageNo,
			@RequestParam(value = "memberId", required = false) String memberId ,//会员id
			@RequestParam(value = "memberName", required = false) String memberName,//会员名称
			@RequestParam(value = "companyId", required = false) String companyId,//企业id
			@RequestParam(value = "companyName", required = false) String companyName,//企业名称
			@RequestParam(value = "hqId", required = false) String hqId,//总部id
			@RequestParam(value = "hqName", required = false) String hqName,//总部名称
			@RequestParam(value = "stationId", required = false) String stationId,//油站id
			@RequestParam(value = "stationName", required = false) String stationName,//油站名称
			HttpSession session, Model model){
		MemberSearchCriteriaTO searchCriteria = new MemberSearchCriteriaTO(memberId, memberName, companyId, companyName,
				hqId, hqName, stationId, stationName, pageSize, pageNo, (pageNo > 0 && pageSize > 0));
		
		List<MemberTO> agentTOs=memberService.getMemberByCustomerTO(searchCriteria);
		int totalCount=memberService.getMemberCntByCriteriaTO(searchCriteria);
		CollectionVO collectionTO=new CollectionVO(agentTOs, totalCount, pageSize, pageNo);
		model.addAttribute("collectionTO", collectionTO);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNumShow", totalCount/pageSize+((totalCount%pageSize>0)?1:0));
		model.addAttribute("searchCriteria", searchCriteria);
		
		return "/member/list";
	}
	/**冻结*/
	@RequestMapping(value="/freeze")
	@ResponseBody
	public String freeze(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "memberId", required = true) String memberId,
			HttpSession session, Model model){
		ResponseJsonBody json = null;
		try {
			memberService.update(memberId);
			json = new ResponseJsonBody("200","删除成功！" , "member_list",
					"navTab", "", "", null);
		} catch (Exception e) {
			json = new ResponseJsonBody("300", e.getMessage(), "member_list",
					"navTab", "closeCurrent", null, null);
			e.printStackTrace();
		}
		return gson.toJson(json) ;
		
	}

}
